package cn.wuxing.mediation.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.wuxing.mediation.common.exception.ServiceException;
import cn.wuxing.mediation.common.utils.StringUtils;
import cn.wuxing.mediation.common.utils.bean.BeanValidators;
import org.springframework.stereotype.Service;
import cn.wuxing.mediation.system.mapper.ShiftSchedulingMapper;
import cn.wuxing.mediation.system.domain.ShiftScheduling;
import cn.wuxing.mediation.system.service.IShiftSchedulingService;

import javax.annotation.Resource;

/**
 * 庭室情况Service业务层处理
 *
 * @author mediation
 * @date 2024-10-14
 */
@Service
public class ShiftSchedulingServiceImpl implements IShiftSchedulingService
{
    @Resource
    private ShiftSchedulingMapper shiftSchedulingMapper;

    /**
     * 查询庭室情况
     *
     * @param id 庭室情况主键
     * @return 庭室情况
     */
    @Override
    public ShiftScheduling selectShiftSchedulingById(Long id)
    {
        return shiftSchedulingMapper.selectShiftSchedulingById(id);
    }

    /**
     * 查询庭室情况列表
     *
     * @param shiftScheduling 庭室情况
     * @return 庭室情况
     */
    @Override
    public List<ShiftScheduling> selectShiftSchedulingList(ShiftScheduling shiftScheduling)
    {
        return shiftSchedulingMapper.selectShiftSchedulingList(shiftScheduling);
    }

    /**
     * 新增庭室情况
     *
     * @param shiftScheduling 庭室情况
     * @return 结果
     */
    @Override
    public int insertShiftScheduling(ShiftScheduling shiftScheduling)
    {
        shiftScheduling.setDelFlag("0");
        shiftScheduling.setCreateDate(new Date());
        return shiftSchedulingMapper.insertShiftScheduling(shiftScheduling);
    }

    /**
     * 修改庭室情况
     *
     * @param shiftScheduling 庭室情况
     * @return 结果
     */
    @Override
    public int updateShiftScheduling(ShiftScheduling shiftScheduling)
    {
        shiftScheduling.setUpdateDate(new Date());
        return shiftSchedulingMapper.updateShiftScheduling(shiftScheduling);
    }

    /**
     * 批量删除庭室情况
     *
     * @param ids 需要删除的庭室情况主键
     * @return 结果
     */
    @Override
    public int deleteShiftSchedulingByIds(Long[] ids)
    {
        return shiftSchedulingMapper.deleteShiftSchedulingByIds(ids);
    }

    /**
     * 删除庭室情况信息
     *
     * @param id 庭室情况主键
     * @return 结果
     */
    @Override
    public int deleteShiftSchedulingById(Long id)
    {
        return shiftSchedulingMapper.deleteShiftSchedulingById(id);
    }

    @Override
    public List<ShiftScheduling> selectShiftSchedulings(ShiftScheduling shiftScheduling) {
        return shiftSchedulingMapper.selectShiftSchedulingLists(shiftScheduling);
    }

    /**
     * 导入庭室情况数据
     *
     * @param shiftSchedulingList 庭室情况数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importShiftScheduling(List<ShiftScheduling> shiftSchedulingList, Boolean updateSupport) {
        if (StringUtils.isNull(shiftSchedulingList) || shiftSchedulingList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ShiftScheduling shiftScheduling : shiftSchedulingList)
        {
            try
            {
                // 验证庭室名称是否重复
                String chambers = shiftScheduling.getChambers();
                ShiftScheduling n = shiftSchedulingMapper.selectByIdChambers(chambers);

                if (StringUtils.isNull(n))
                {
                    if(!shiftScheduling.getChambersStatus().equals("")){
                        if(shiftScheduling.getChambersStatus().equals("待调解")){
                            shiftScheduling.setChambersStatus("0");
                        }
                        if(shiftScheduling.getChambersStatus().equals("已调解")){
                            shiftScheduling.setChambersStatus("1");
                        }
                    }
                    shiftScheduling.setCreateDate(new Date());
                    shiftScheduling.setDelFlag("0");
                    shiftSchedulingMapper.insertShiftScheduling(shiftScheduling);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、庭室名称" + shiftScheduling.getChambers() + " 导入成功");
                }
                else if (updateSupport)
                {
                    shiftScheduling.setId(n.getId());
                    if(!shiftScheduling.getChambersStatus().equals("")){
                        if(shiftScheduling.getChambersStatus().equals("待调解")){
                            shiftScheduling.setChambersStatus("0");
                        }
                        if(shiftScheduling.getChambersStatus().equals("已调解")){
                            shiftScheduling.setChambersStatus("1");
                        }
                    }
                    shiftScheduling.setUpdateDate(new Date());
                    shiftScheduling.setDelFlag("0");
                    shiftSchedulingMapper.updateShiftScheduling(shiftScheduling);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、庭室名称 " + shiftScheduling.getChambers() + " 更新成功");
                }
                else if(!StringUtils.isNull(n))
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、庭室名称:"+shiftScheduling.getChambers()+"已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、庭室名称 " + shiftScheduling.getChambers() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
